home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / mflms101.arc / DUMP.C < prev    next >
C/C++ Source or Header  |  1989-11-25  |  8KB  |  228 lines

  1. /*
  2. **                HEX FILE DUMP UTILITY
  3. **   Copyright 1987-89  Steven E. Margison
  4. **   Displays any file in hex and character representation
  5. **   in color or monochrome, depending upon type of video
  6. **   card installed.  Displays 20 lines per screen and waits
  7. **   for a keypress.
  8. **
  9. **   Rev 1.10   02-25-89 for Turbo-C 2.0  S.E. Margison
  10. **
  11. **   Modified 1989 by Bob Stout
  12. **
  13. **   As distributed, this program requires (for compilation):
  14. **     "The MicroFirm Function LIbrary for MS/QC"
  15. **   which may be obtained without registration from many Bulletin
  16. **   Board Systems.
  17. **
  18. **   or by registration:
  19. **      $25 for Docs, C, S, M, L, H libraries, and complete library source
  20. **              in C and Assembler
  21. **     MicroFirm
  22. **     P.O. Box 428
  23. **     Alief, TX 77411
  24. **
  25. **
  26. ** Program requires at least one parameter, the name of the file to dump.
  27. ** Other options:
  28. **     -snnnn        Start at hex offset nnnn into file (default is 0000)
  29. **     -hnn          Highlite all bytes of hex value nn in display
  30. **     -r            Right side display is actual IBM screen character.
  31. **                    Without this option, only valid ASCII characters
  32. **                    are displayed.  All others display as a period.
  33. **
  34. ** More than a utility, this is a good demonstration of the direct
  35. ** video subroutines and video page switching mechanism (for CGA cards).
  36. ** Look upon this as a tutorial as well as a handy utility.
  37. */
  38.  
  39. #include <stdio.h>
  40. #include <ctype.h>
  41. #include <mflconio.h>
  42. #include <mflfiles.h>
  43. #include <mflsys.h>
  44.  
  45. int adrclr,    /* address field color */
  46.     hexclr,    /* hex data color */
  47.     highclr,   /* highlite color */
  48.     prompt,    /* prompt line color */
  49.     ascclr;    /* ascii color */
  50.  
  51. int buffer[16];
  52. unsigned long offset = 0;
  53. FILE *f = NULL;
  54.  
  55. int mono, cpage, eoflag, rawflag;
  56.  
  57.  
  58. void bad_options(void)
  59. {
  60.         error("Use: dump file [-snnnn -hnn -r]");
  61. }
  62.  
  63.  
  64. main(int argc, char *argv[])
  65. {
  66.         int i, lines, view, vflag;
  67.  
  68.         eoflag = vflag = FALSE;      /* default highlite off */
  69.         rawflag = FALSE;             /* default normal display mode */
  70.  
  71.         if ((argc < 2) || (argc > 4))
  72.                 bad_options();
  73.  
  74.         while (--argc > 0)
  75.         {
  76.                 if (argv[argc][0] == '-')
  77.                 {
  78.                         switch(argv[argc][1])
  79.                         {
  80.                         case 'r':
  81.                         case 'R':
  82.                                 rawflag = TRUE;
  83.                                 break;
  84.                         case 'h':
  85.                         case 'H':
  86.                                 sscanf(&argv[argc][2], "%02x", &view);
  87.                                 vflag = TRUE;
  88.                                 break;
  89.                         case 's':
  90.                         case 'S':
  91.                                 sscanf(&argv[argc][2], "%lx", &offset);
  92.                                 break;
  93.                         }     /* end of switch */
  94.                         continue;
  95.                 }
  96.                 else
  97.                 {
  98.                         if (f != NULL)
  99.                                 error("Too many filenames specified");
  100.                         if ((f = fopen(argv[argc], "rb")) == NULL)
  101.                                 cant(argv[argc]);
  102.                 }
  103.         }   /* end of while statement */
  104.  
  105.         if (f == NULL)
  106.                 bad_options();
  107.         fseek(f, offset, 0);
  108.  
  109.         i = stuff(0);   /* check video type */
  110.         if (i == MONO)
  111.         {
  112.                 mono = TRUE;
  113.                 prompt = BLINKING;
  114.                 adrclr = WHITE;
  115.                 hexclr = HIWHITE;
  116.                 ascclr = WHITE;
  117.                 highclr = HIGHBLINK;
  118.         }
  119.         else
  120.         {         /* for CGA cards */
  121.                 vmode(CLR80);
  122.                 mono = FALSE;
  123.                 cpage = 1;       /* we'll write to page 1 first */
  124.                 prompt = BROWN;
  125.                 adrclr = LTGREEN;
  126.                 hexclr = YELLOW;
  127.                 ascclr = LTCYAN;
  128.                 highclr = HIWHITE;
  129.         }
  130.  
  131.         dvid_init();    /* use direct video access routines */
  132.         dvid_cls();
  133.         dvid_move(0,0);
  134.         dvid_flush();
  135.         dvid_attrib(hexclr);   /* set hex value color, our default */
  136.         if (!mono)
  137.         {
  138.                 dvid_setpage(0, 1);       /* set page 0 as display page */
  139.                 dvid_setpage(1, 0);       /* but set page 1 as writing page */
  140.         }
  141.         cursor_style(5, 0, 0);    /* kill the cursor */
  142.         lines = 20;
  143.  
  144.         for EVER
  145.         {
  146.                 /* if this were a level 1 file using read() and open()
  147.                         ** the program would be faster.  Just a hint! */
  148.                 for (i = 0; i < 16; i++)
  149.                         buffer[i] = fgetc(f);
  150.  
  151.                 if (buffer[0] == -1)
  152.                 {
  153.                         if (lines == 20)
  154.                                 break;    /* we must've ended on even page */
  155.                         eoflag = TRUE;
  156.                         goto alldone;
  157.                 }
  158.  
  159.                 dvid_printfa(adrclr, "%04lx: ",offset);  /* display address */
  160.                 for (i = 0; i < 16; i++)
  161.                 {
  162.                         if (buffer[i] != -1)
  163.                         {
  164.                                 if (vflag && (buffer[i] == view))
  165.                                 {       /* highlite this byte? */
  166.                                         dvid_printfa(highclr, "%02x ",
  167.                                                 buffer[i]);
  168.                                 }
  169.                                 else    dvid_printf("%02x ", buffer[i]);
  170.                         }
  171.                         else    dvid_putstr("   ");
  172.                 }
  173.                 dvid_raw(rawflag);     /* select our mode */
  174.                 dvid_attrib(ascclr);  /* display ascii data color */
  175.                 dvid_putstr("   ");
  176.                 for (i = 0; i < 16; i++)
  177.                 {
  178.                         if (buffer[i] != -1)
  179.                         {
  180.                                 if (!isprint(buffer[i]) && !rawflag)
  181.                                         dvid_putchr('.');
  182.                                 else    dvid_putchr(buffer[i]);
  183.                         }
  184.                         else    dvid_putchr(' ');
  185.                 }
  186.                 dvid_raw(FALSE);         /* force non-raw mode */
  187.                 dvid_putchr('\n');
  188.                 if (lines-- == 0)
  189.                 {
  190. alldone:                dvid_attrib(prompt);
  191.                         dvid_say(22, 10, "Press any key to continue, "
  192.                                 "ESCape to quit...");
  193.                         if (!mono)
  194.                                 dvid_setpage(cpage, 1); /* display this page */
  195.                         if (getkey() == ESC)
  196.                         {
  197.                                 dvid_setpage(0, 1);     /* return to page 0 */
  198.                                 if (!mono)              /* restore cursor */
  199.                                         cursor_style(1, 0, 0);
  200.                                 else    cursor_style(2, 0, 0);
  201.                                 aabort(1);
  202.                         }
  203.                         if (eoflag)
  204.                                 break;
  205.                         cpage++;     /* bump page #, maintain 0-3 */
  206.                         cpage &= 3;
  207.  
  208.                         if (!mono)  /* set the next page for write */
  209.                                 dvid_setpage(cpage, 0);
  210.                         dvid_cls();
  211.                         dvid_flush();
  212.                         lines = 20;
  213.                 }
  214.                 offset += 16;
  215.                 dvid_attrib(hexclr);
  216.         }  /* end of forever loop */
  217.  
  218.         dvid_setpage(0, 1);   /* restore page 0 */
  219.         dvid_cls();
  220.         dvid_attrib(prompt);
  221.         dvid_say(22, 10, "End of File Encountered");
  222.         if (!mono)
  223.                 cursor_style(1, 0, 0);  /* restore cursor */
  224.         else cursor_style(2, 0, 0);
  225.         dvid_flush();
  226.         dvid_done();
  227. }
  228.